package com.ngovanhau.justsort.model;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Context;

import com.ngovanhau.justsort.MainGameActivity;
import com.ngovanhau.justsort.MySprite;
import com.ngovanhau.justsort.control.ValueControl;
import com.ngovanhau.justsort.item.config.MyConfig;

public class ButtonBackGame extends MySprite {

	private BitmapTextureAtlas btn_setting_BTA;
	private Sprite btn_setting_SP;
	private TextureRegion btn_setting_TR;

	int Y_axis = 0, X_axis = 0;

	public void onLoadResource(Engine enginex, Context contextx) {
		this.engine = enginex;
		this.context = contextx;
		this.mainGame = (MainGameActivity) contextx;

		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("images/");

		this.btn_setting_BTA = new BitmapTextureAtlas(512, 512);
		this.btn_setting_TR = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.btn_setting_BTA, contextx,
						"game_btn_back.png", 0, 0);
		this.engine.getTextureManager().loadTexture(btn_setting_BTA);

	}

	public void onLoadScene(Scene scene) {
		this.scene = scene;

		// bottom
		float h = (int) (btn_setting_TR.getHeight() * MyConfig.RACE_HEIGHT);
		float w = (int) (btn_setting_TR.getWidth() * MyConfig.RACE_WIDTH);
		Y_axis = (int) (MyConfig.SCREENHEIGHT - h);
		this.btn_setting_SP = new Sprite(0, Y_axis, w, h, btn_setting_TR) {
			@Override
			public boolean onAreaTouched(final TouchEvent pTouchEvent,
					final float pTouchLocalX, final float pTouchLocalY) {
				if (ValueControl.isTouchNumber) {
					ValueControl.isCompletedLevel = true;
					mainGame.finish();
				}
				return true;
			}
		};
		scene.attachChild(btn_setting_SP);
		scene.registerTouchArea(btn_setting_SP);
		scene.setZIndex(1);
		scene.sortChildren();
	}
}
